#include "keyboard.h"
#include "globalconst.h"

KeyBoard* KeyBoard::instance = 0;

KeyBoard::KeyBoard(QObject *parent) :
        QObject(parent), KBSR(GlobalConst::EMPTY_REGISTER), KBDR(GlobalConst::EMPTY_REGISTER)
{
}

KeyBoard::~KeyBoard()
{
}

KeyBoard* KeyBoard::getInstance(QObject *parent)
{
    if(instance == 0) {
        instance = new KeyBoard(parent);
    }
    return instance;
}

QString KeyBoard::getKBDR(int partIndex)
{
    QString copy = KBDR.mid(8 * partIndex, 8);
    if(partIndex == 3)
    {
        //Clear KBSR.
        KBSR.replace(31, 1, "0");
        //
        emit keyBoardAvailable();
    }
    return copy;
}

QString KeyBoard::getKBSR(int partIndex) const
{
    return KBSR.mid(8 * partIndex, 8);
}

void KeyBoard::inputChar(QChar letter)
{
    int byte = static_cast<int>(letter.toAscii());
    QString binary;
    binary.setNum(byte, 2);
    while(binary.size() < 32)
    {
        binary.prepend("0");
    }
    KBDR = binary;
    //Set KBSR.
    KBSR.replace(31, 1, "1");
}
